package com.qixin.modules.eaApplication.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qixin.modules.ea.entity.EaEntertain;
import com.qixin.modules.ea.entity.EaEntertainReceptionActivity;
import com.qixin.modules.ea.mapper.EaEntertainMapper;
import com.qixin.modules.ea.mapper.EaEntertainReceptionActivityMapper;
import com.qixin.modules.ea.mapper.EaSysUserMapper;
import com.qixin.modules.ea.vo.EaSysUser;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaApplication.entity.*;
import com.qixin.modules.eaApplication.mapper.EaBusinessApplyMapper;
import com.qixin.modules.eaApplication.mapper.EaEntertainApplyMapper;
import com.qixin.modules.eaApplication.mapper.EaHostObjectMapper;
import com.qixin.modules.eaApplication.mapper.EaEntertainChangeMapper;
import com.qixin.modules.eaApplication.service.IEaEntertainChangeService;
import com.qixin.modules.eaBasic.entity.EaRegion;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaRegionMapper;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaBasic.util.WorkFlowUtil;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.UserIdPageVo;
import com.qixin.modules.eaLoan.entity.EaLoanbillDetail;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;

/**
 * @Description: 招待费申请变更单
 * @Author: qixin-boot
 * @Date:   2021-07-06
 * @Version: V1.0
 */
@Service
public class EaEntertainChangeServiceImpl extends ServiceImpl<EaEntertainChangeMapper, EaEntertainChange> implements IEaEntertainChangeService {

	@Autowired
	private EaEntertainChangeMapper eaEntertainChangeMapper;
	@Autowired
	private EaTaskMapper eaTaskMapper;
	@Autowired
	private ChangeIdToNameUtil changeIdToNameUtil;
	@Autowired
	private EaSysUserMapper eaSysUserMapper;
	@Autowired
	private EaEntertainApplyMapper eaEntertainApplyMapper;
	@Autowired
	private EaHostObjectMapper eaHostObjectMapper;
	@Value(value = "${server.fileurl}")
	private String fileurl;
	@Autowired
	private WorkFlowUtil workFlowUtil;
	@Autowired
	private EaEntertainReceptionActivityMapper eaEntertainReceptionActivityMapper;

	//新增出差申请变更单
	@Override
	@Transactional
	public Map<String,Object> saveMain(EaEntertainChange eaEntertainChange,List<EaHostObject> eaHostObjectList,List<EaEntertainReceptionActivity> eaEntertainReceptionActivities) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		Object result = FillRuleUtil.executeRule("entertain_change_code", null);  //获取单据编号
		eaEntertainChange.setBillNo(result.toString());

		String saveType = eaEntertainChange.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			eaEntertainChange.setBillStatus(0);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaEntertainChange.setBillStatus(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		eaEntertainChange.setId(Uuid32Util.getUUid());  //主键
		eaEntertainChangeMapper.insert(eaEntertainChange);

		//更新相应的招待费申请单为正在变更状态
		String applyBillNo = EmptyUtil.checkNull(eaEntertainChange.getApplyBillNo());
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("bill_no",applyBillNo);
		List<EaEntertainApply> eaEntertainApplyList = eaEntertainApplyMapper.selectByMap(params);
		if(eaEntertainApplyList != null && eaEntertainApplyList.size() > 0){
			EaEntertainApply eaEntertainApply = eaEntertainApplyList.get(0);
			if(eaEntertainApply.getIsChanging().equals("1")){
				throw new RuntimeException("招待费申请单:" + eaEntertainApply.getBillNo() + "已申请变更,请选择其他单据!");
			}
			eaEntertainApply.setIsChanging("1");    //是否变更状态
			eaEntertainApplyMapper.updateById(eaEntertainApply);
		}

		//保存招待对象明细表
		if(eaHostObjectList != null && eaHostObjectList.size() > 0){
			for (int i = 0; i < eaHostObjectList.size(); i++) {
				EaHostObject eaHostObject = eaHostObjectList.get(i);
				eaHostObject.setId(Uuid32Util.getUUid());
				eaHostObject.setEntertainApplyId(eaEntertainChange.getId());     //招待费变更单id
				eaHostObjectMapper.insert(eaHostObject);
			}
		}
		if(eaEntertainReceptionActivities!=null && eaEntertainReceptionActivities.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivities) {
				//新增活动明细
				entity.setPid(eaEntertainChange.getId());
				entity.setId(Uuid32Util.getUUid());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}

		//新增待办
		EaTask eaTask = new EaTask();
		eaTask.setId(Uuid32Util.getUUid());
		eaTask.setFormType("7");  //单据类型:1差旅费报销单,2借款单,3还款单,4出差申请单,5出差申请变更单,6招待费申请单,7招待费申请变更单,8招待费报销单,9资金支付审批单,10通讯报销单,11电话记录单
		eaTask.setAuditManId(eaEntertainChange.getAuditManId());   //审批人id
		eaTask.setAuditMan(eaEntertainChange.getAuditMan());    //审批人
		//提交类型:保存:add,提交:send
		if(saveType.equals("add")){  //add保存,send提交
			eaTask.setStatus("0");
		}else{
			eaTask.setStatus("1");
		}
		eaTask.setFormId(eaEntertainChange.getId());    //单据id
		eaTask.setFormCode(eaEntertainChange.getBillNo());   //单据编号
		eaTask.setMemo(eaEntertainChange.getCause());    //备注

		//根据招待费申请变更单编号去查询招待费申请变更单,获取相应的总金额
		eaTask.setTotalFee(eaEntertainChange.getAmount()); //总金额
		eaTask.setSort("7");   //排序
		eaTask.setTitle("招待费申请变更单");  //标题
		eaTask.setSendManId(eaEntertainChange.getPersonId());    //发送人id
		eaTask.setSendMan(changeIdToNameUtil.getNameById(eaEntertainChange.getPersonId()));    //发送人

		//根据用户id获取用户部门
		EaSysUser eaSysUser = eaSysUserMapper.selectById(eaEntertainChange.getPersonId());

		eaTask.setSendDepartId(eaSysUser.getDepartIds());  //发送人部门id
		eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaSysUser.getDepartIds()));   //发送人部门
		eaTask.setSendTime(new Date());    //发送时间
		eaTaskMapper.insert(eaTask);

		returnMap.put("id",eaEntertainChange.getId());
		returnMap.put("code","0");
		returnMap.put("message","保存成功!");
		return returnMap;
	}

	//编辑出差申请变更单
	@Override
	@Transactional
	public Map<String,Object> updateMain(EaEntertainChange eaEntertainChange,List<EaHostObject> eaHostObjectList,List<EaEntertainReceptionActivity> eaEntertainReceptionActivities) {
		Map<String,Object> returnMap = new HashMap<String,Object>();

		//如果picture中有http://localhost:9092/qixin-boot/aaa.png,要处理只保留文件名
		String picture = eaEntertainChange.getPicture() != null ? eaEntertainChange.getPicture().toString() : "";
		String picture_final = picture.replaceAll(fileurl,"");;
		//设置最终的图片如:1111.png,2222.png
		eaEntertainChange.setPicture(picture_final);

		String saveType = eaEntertainChange.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			EaEntertainChange eaEntertainChange1 = eaEntertainChangeMapper.selectById(eaEntertainChange.getId());
			eaEntertainChange.setBillStatus(eaEntertainChange1.getBillStatus());   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaEntertainChange.setBillStatus(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		//将旧的关联申请单变更状态改成0
		EaEntertainChange eaEntertainChange3  = eaEntertainChangeMapper.selectById(eaEntertainChange.getId());
		String applyBillNo3 = EmptyUtil.checkNull(eaEntertainChange3.getApplyBillNo());
		Map<String,Object> params3 = new HashMap<String,Object>();
		params3.put("bill_no",applyBillNo3);
		List<EaEntertainApply> eaEntertainApplyList3 = eaEntertainApplyMapper.selectByMap(params3);
		if(eaEntertainApplyList3 != null && eaEntertainApplyList3.size() > 0){
			EaEntertainApply eaEntertainApply = eaEntertainApplyList3.get(0);
			eaEntertainApply.setIsChanging("0");    //是否变更状态
			eaEntertainApplyMapper.updateById(eaEntertainApply);
		}

		eaEntertainChangeMapper.updateById(eaEntertainChange);

		//先删除相应的招待对象明细表
		Map<String,Object> params2 = new HashMap<String,Object>();
		params2.put("entertain_apply_id",eaEntertainChange.getId());
		eaHostObjectMapper.deleteByMap(params2);
		//删除活动明细
		Map<String,Object> activitiParams = new HashMap<String,Object>();
		activitiParams.put("pid",eaEntertainChange.getId());
		eaEntertainReceptionActivityMapper.deleteByMap(activitiParams);

		//更新相应的招待费申请单为正在变更状态
		String applyBillNo = EmptyUtil.checkNull(eaEntertainChange.getApplyBillNo());
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("bill_no",applyBillNo);
		List<EaEntertainApply> eaEntertainApplyList = eaEntertainApplyMapper.selectByMap(params);
		if(eaEntertainApplyList != null && eaEntertainApplyList.size() > 0){
			EaEntertainApply eaEntertainApply = eaEntertainApplyList.get(0);
			if(eaEntertainApply.getIsChanging().equals("1")){
				throw new RuntimeException("招待费申请单:" + eaEntertainApply.getBillNo() + "已申请变更,请选择其他单据!");
			}
			eaEntertainApply.setIsChanging("1");    //是否变更状态
			eaEntertainApplyMapper.updateById(eaEntertainApply);
		}

		//再保存招待对象明细表
		if(eaHostObjectList != null && eaHostObjectList.size() > 0){
			for (int i = 0; i < eaHostObjectList.size(); i++) {
				EaHostObject eaHostObject = eaHostObjectList.get(i);
				eaHostObject.setId(Uuid32Util.getUUid());
				eaHostObject.setEntertainApplyId(eaEntertainChange.getId());     //招待费变更单id
				eaHostObjectMapper.insert(eaHostObject);
			}
		}
		if(eaEntertainReceptionActivities!=null && eaEntertainReceptionActivities.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivities) {
				//新增活动明细
				entity.setPid(eaEntertainChange.getId());
				entity.setId(Uuid32Util.getUUid());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}

		//修改待办
		Map<String,Object> paramsMap2 = new HashMap<String,Object>();
		paramsMap2.put("form_id",eaEntertainChange.getId());
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap2);
		for (EaTask eaTask:eaTaskList) {
			eaTask.setAuditManId(eaEntertainChange.getAuditManId());   //审批人id
			eaTask.setAuditMan(eaEntertainChange.getAuditMan());    //审批人
			//提交类型:保存:add,提交:send
			if(saveType.equals("add")){  //add保存,send提交
				eaTask.setStatus("0");
			}else{
				eaTask.setStatus("1");
			}
			eaTask.setFormId(eaEntertainChange.getId());    //单据id
			eaTask.setFormCode(eaEntertainChange.getBillNo());   //单据编号
			eaTask.setMemo(eaEntertainChange.getCause());    //备注

			//根据招待费申请变更单编号去查询招待费申请变更单,获取相应的总金额
			eaTask.setTotalFee(eaEntertainChange.getAmount()); //总金额
			eaTask.setSendManId(eaEntertainChange.getPersonId());    //发送人id
			eaTask.setSendMan(changeIdToNameUtil.getPsnNameById(eaEntertainChange.getPersonId()));    //发送人

			//根据用户id获取用户部门
			EaSysUser eaSysUser = eaSysUserMapper.selectById(eaEntertainChange.getPersonId());

			eaTask.setSendDepartId(eaSysUser.getDepartIds());  //发送人部门id
			eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaSysUser.getDepartIds()));   //发送人部门
			eaTask.setSendTime(new Date());    //发送时间
			eaTaskMapper.updateById(eaTask);
		}

		returnMap.put("id",eaEntertainChange.getId());
		returnMap.put("code","0");
		returnMap.put("message","编辑成功!");
		return returnMap;
	}

	@Override
	@Transactional
	public void delMain(String id) {
		EaEntertainChange eaEntertainChange = eaEntertainChangeMapper.selectById(id);

		//将关联的申请单变更状态变成0
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("bill_no",eaEntertainChange.getApplyBillNo());
		List<EaEntertainApply> eaBusinessApplyList = eaEntertainApplyMapper.selectByMap(params);
		if(eaBusinessApplyList.size() > 0){
			for (EaEntertainApply eaEntertainApply: eaBusinessApplyList) {
				eaEntertainApply.setIsChanging("0");      //未变更状态
				eaEntertainApplyMapper.updateById(eaEntertainApply);
			}
		}

		eaEntertainChangeMapper.deleteById(id);
		Map<String,Object> params2 = new HashMap<String,Object>();
		params2.put("entertain_apply_id",eaEntertainChange.getId());
		eaHostObjectMapper.deleteByMap(params2);
		//删除活动明细
		Map<String,Object> activitiParams = new HashMap<String,Object>();
		activitiParams.put("pid",eaEntertainChange.getId());
		eaEntertainReceptionActivityMapper.deleteByMap(activitiParams);

		//删除相应的待办
		Map<String,Object> paramaMap = new HashMap<String,Object>();
		paramaMap.put("form_id",id);
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramaMap);
		if(eaTaskList != null && eaTaskList.size() > 0){
			EaTask eaTask = eaTaskList.get(0);

			//删除相应的工作流
			workFlowUtil.deleteWorkFlowById(eaTask.getWorkflowTaskId(),eaTask.getProcinstId());

			eaTaskMapper.deleteById(eaTask.getId());
		}
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaEntertainChangeMapper.deleteById(id);
		}
	}

	//查询招待费申请变更单列表
	@Override
	public Map<String, Object> list(ListPageVo listPageVo) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<String,Object>();
		String page = EmptyUtil.checkNull(listPageVo.getPage());
		String limit = EmptyUtil.checkNull(listPageVo.getLimit());
		String keyword = EmptyUtil.checkNull(listPageVo.getKeyword());
		String userid = EmptyUtil.checkNull(loginUser.getId());   //用户id
		String status = EmptyUtil.checkNull(listPageVo.getStatus());   //状态
		String billNo = EmptyUtil.checkNull(listPageVo.getBillNo());   //单据编号(pc)
		String billDateBegin = EmptyUtil.checkNull(listPageVo.getBillDateBegin());   //单据日期-开始(pc)
		String billDateEnd = EmptyUtil.checkNull(listPageVo.getBillDateEnd());   //单据日期-结束(pc)
		String personId = EmptyUtil.checkNull(listPageVo.getPersonId());   //申请人/报销人/借款人/还款人(pc)
		String explainStr = EmptyUtil.checkNull(listPageVo.getCasue());   //事由(pc)
		String sysOrgCode = EmptyUtil.checkNull(listPageVo.getSysOrgCode());   //所属组织(pc)
		String keyword1 = "%" + keyword + "%";
		map.put("keyword",keyword1);
		map.put("page",(Integer.parseInt(page) -1) * Integer.parseInt(limit));
		map.put("limit",Integer.parseInt(limit));
		map.put("userid",userid);
		map.put("status",status);
		map.put("billNo",billNo);
		map.put("billDateBegin",billDateBegin);
		map.put("billDateEnd",billDateEnd);
		map.put("personId",personId);
		map.put("explainStr",explainStr);
		map.put("sysOrgCode",sysOrgCode);
		List<EaEntertainChange> eaEntertainChangeList = eaEntertainChangeMapper.selectEntertainChanges(map);
		List<Map<String,Object>> returnList = new ArrayList<Map<String,Object>>();
		if(eaEntertainChangeList != null && eaEntertainChangeList.size() > 0){
			for (EaEntertainChange eaEntertainChange:eaEntertainChangeList) {
				Map<String,Object> returnMap = new HashMap<String,Object>();
				//将实体数据存到map
				returnMap = JSON.parseObject(JSON.toJSONString(eaEntertainChange, SerializerFeature.WriteMapNullValue), Map.class);
				//格式化时间
				Long createTime = returnMap.get("createTime") != null ? Long.parseLong(returnMap.get("createTime").toString()) : null;
				String createTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(createTime);
				returnMap.put("createTime",createTimeStr);

				//格式化时间
				Long updateTime = returnMap.get("updateTime") != null ? Long.parseLong(returnMap.get("updateTime").toString()) : null;
				String updateTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(updateTime);
				returnMap.put("updateTime",updateTimeStr);

				//更新时间-用于校验时效性
				map.put("time",updateTimeStr);

				//格式化时间
				Long billDate = returnMap.get("billDate") != null ? Long.parseLong(returnMap.get("billDate").toString()) : null;
				String billDateStr = DateFormatUtil.formatDateyyMMdd(billDate);
				returnMap.put("billDate",billDateStr);

				//格式化时间
				Long receptionTime = returnMap.get("receptionTime") != null ? Long.parseLong(returnMap.get("receptionTime").toString()) : null;
				String receptionTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(receptionTime);
				returnMap.put("receptionTime",receptionTimeStr);

				//格式化时间 - 用餐安排_时间
				Long mealArrangementDate = returnMap.get("mealArrangementDate") != null ? Long.parseLong(returnMap.get("mealArrangementDate").toString()) : null;
				String mealArrangementDateStr = DateFormatUtil.formatDateyyMMdd(mealArrangementDate);
				returnMap.put("mealArrangementDate",mealArrangementDateStr);

				returnList.add(returnMap);
			}
		}

		int count = 0;
		map.put("page",0);
		map.put("limit",0);
		map.put("keyword",keyword1);
		map.put("userid",userid);
		List<EaEntertainChange> list2 = eaEntertainChangeMapper.selectEntertainChanges(map);
		count = list2.size();
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("records",returnList);
		returnMap.put("total",count);
		return returnMap;
	}

	//查询出差申请变更单详情
	@Override
	public Map<String, Object> selectById(String id) {
		//查询出差申请变更单详情
		Map<String,Object> queryWrapper = new HashMap<String,Object>();
		queryWrapper.put("id",id);  //出差申请变更单id
		queryWrapper.put("page",0);
		queryWrapper.put("limit",0);
		List<EaEntertainChange> eaEntertainChangeList = eaEntertainChangeMapper.selectEntertainChanges(queryWrapper);
		Map<String,Object> returnMap = new HashMap<String,Object>();
		if(eaEntertainChangeList != null && eaEntertainChangeList.size() > 0){
			EaEntertainChange eaEntertainChange = eaEntertainChangeList.get(0);

			//将实体数据存到map
			returnMap = JSON.parseObject(JSON.toJSONString(eaEntertainChange, SerializerFeature.WriteMapNullValue), Map.class);

			//格式化时间
			Long createTime = returnMap.get("createTime") != null ? Long.parseLong(returnMap.get("createTime").toString()) : null;
			String createTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(createTime);
			returnMap.put("createTime",createTimeStr);

			//格式化时间
			Long updateTime = returnMap.get("updateTime") != null ? Long.parseLong(returnMap.get("updateTime").toString()) : null;
			String updateTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(updateTime);
			returnMap.put("updateTime",updateTimeStr);

			//更新时间-用于校验时效性
			returnMap.put("time",updateTimeStr);

			//格式化时间
			Long billDate = returnMap.get("billDate") != null ? Long.parseLong(returnMap.get("billDate").toString()) : null;
			String billDateStr = DateFormatUtil.formatDateyyMMdd(billDate);
			returnMap.put("billDate",billDateStr);

			//格式化时间
			Long receptionTime = returnMap.get("receptionTime") != null ? Long.parseLong(returnMap.get("receptionTime").toString()) : null;
			String receptionTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(receptionTime);
			returnMap.put("receptionTime",receptionTimeStr);

			//格式化时间 - 用餐安排_时间
			Long mealArrangementDate = returnMap.get("mealArrangementDate") != null ? Long.parseLong(returnMap.get("mealArrangementDate").toString()) : null;
			String mealArrangementDateStr = DateFormatUtil.formatDateyyMMdd(mealArrangementDate);
			returnMap.put("mealArrangementDate",mealArrangementDateStr);

			//添加图片
			String picture = returnMap.get("picture") != null ? returnMap.get("picture").toString() : "";
			List<Map<String,Object>> fileList = GetUrlUtil.getUrl(picture,fileurl);

			//添加图片fileList
			returnMap.put("fileList",fileList);

            //查询招待费明细信息
            Map<String,Object> eaHostObjectMap = new HashMap<String,Object>();
            eaHostObjectMap.put("entertain_apply_id",id);
            List<EaHostObject> eaHostObjectList = eaHostObjectMapper.selectByMap(eaHostObjectMap);
            List<Map<String,Object>> eaHostObjectMapList = new ArrayList<Map<String,Object>>();
            if(eaHostObjectList != null && eaHostObjectList.size() > 0){
                for (int i = 0; i < eaHostObjectList.size(); i++) {
                    Map<String,Object> eaHostObjectMap2 = JSON.parseObject(JSON.toJSONString(eaHostObjectList.get(i), SerializerFeature.WriteMapNullValue), Map.class);
                    //创建人
                    eaHostObjectMap2.put("createByName",changeIdToNameUtil.getNameById(EmptyUtil.checkNull(eaHostObjectMap2.get("createBy"))));
                    //更新人
                    eaHostObjectMap2.put("updateByName",changeIdToNameUtil.getNameById(EmptyUtil.checkNull(eaHostObjectMap2.get("updateBy"))));
                    //所属部门
                    eaHostObjectMap2.put("sysOrgCodeName",changeIdToNameUtil.getNameByDepartId(EmptyUtil.checkNull(eaHostObjectMap2.get("sysOrgCode"))));

                    //格式化时间
                    Long createTime1 = eaHostObjectMap2.get("createTime") != null ? Long.parseLong(eaHostObjectMap2.get("createTime").toString()) : null;
                    String createTime1Str = DateFormatUtil.formatDateyyMMddHHmmss(createTime1);
                    eaHostObjectMap2.put("createTime",createTime1Str);

                    //格式化时间
                    Long updateTime1 = eaHostObjectMap2.get("updateTime") != null ? Long.parseLong(eaHostObjectMap2.get("updateTime").toString()) : null;
                    String updateTime1Str = DateFormatUtil.formatDateyyMMddHHmmss(updateTime1);
                    eaHostObjectMap2.put("updateTime",updateTime1Str);

                    //添加借款单明细
                    eaHostObjectMapList.add(eaHostObjectMap2);
                }
            }

            //将明细信息加入返回节点
            returnMap.put("eaHostObjectList",eaHostObjectMapList);

			//添加相应的活动明细
			Map<String,Object> activitiParams = new HashMap<String,Object>();
			activitiParams.put("pid",id);
			List<EaEntertainReceptionActivity> eaEntertainReceptionActivities = eaEntertainReceptionActivityMapper.selectByMap(activitiParams);
			List<Map<String,Object>> mapList6 = new ArrayList<Map<String,Object>>();
			if(eaEntertainReceptionActivities.size() > 0){
				for (EaEntertainReceptionActivity eaEntertainReceptionActivity: eaEntertainReceptionActivities) {
					Map map6 = JSON.parseObject(JSON.toJSONString(eaEntertainReceptionActivity, SerializerFeature.WriteMapNullValue), Map.class);

					//格式化时间
					Long date = map6.get("date") != null ? Long.parseLong(map6.get("date").toString()) : null;
					String dateStr = DateFormatUtil.formatDateyyMMdd(date);
					map6.put("date",dateStr);

					mapList6.add(map6);
				}
			}

			//将活动明细加到eaEntertainReceptionActivities节点
			returnMap.put("eaEntertainReceptionActivities",mapList6);
		}
		return returnMap;
	}

	@Override
	@Transactional
	public Map<String, Object> audit(EaTaskVo eaTaskVo) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		String id = EmptyUtil.checkNull(eaTaskVo.getId());   //单据id
//        String auditManId = EmptyUtil.checkNull(eaTaskVo.getAuditManId());   //审批人id
//        String auditMan = EmptyUtil.checkNull(eaTaskVo.getAuditMan());   //审批人
		String auditComment = EmptyUtil.checkNull(eaTaskVo.getAuditComment());   //审批意见
		String status = EmptyUtil.checkNull(eaTaskVo.getStatus());   //单据状态:2通过,3退回

		//获取出差申请变更单-更新状态
		EaEntertainChange eaEntertainChange = eaEntertainChangeMapper.selectById(id);
		eaEntertainChange.setBillStatus(Integer.parseInt(status));
		eaEntertainChangeMapper.updateById(eaEntertainChange);

		//当出差申请变更单审核通过后,要将修改信息同步到相应的招待费申请变更单
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("bill_no",eaEntertainChange.getApplyBillNo());
		List<EaEntertainApply> eaEntertainApplyList = eaEntertainApplyMapper.selectByMap(params);
		if(eaEntertainApplyList != null && eaEntertainApplyList.size() > 0){
			EaEntertainApply eaEntertainApply = eaEntertainApplyList.get(0);
			eaEntertainApply.setIsChanging("0");   //是否变更状态
			if(status.equals("2")){
				eaEntertainApply.setCostCompCodeId(eaEntertainChange.getCostCompCodeId());    //费用承担单位
				eaEntertainApply.setCostDept(eaEntertainChange.getCostDept());    //费用承担部门
				eaEntertainApply.setReceptionType(eaEntertainChange.getReceptionType());    //接待类型
				eaEntertainApply.setReceptionTime(eaEntertainChange.getReceptionTime());    //接待时间
				eaEntertainApply.setReceptionMan(eaEntertainChange.getReceptionMan());    //接待人数
				eaEntertainApply.setReceptionName(eaEntertainChange.getReceptionName());    //联系人姓名
				eaEntertainApply.setReceptionPhone(eaEntertainChange.getReceptionPhone());    //联系人电话
				eaEntertainApply.setCause(eaEntertainChange.getCause());    //事由
				eaEntertainApply.setVehicleArrangement(eaEntertainChange.getVehicleArrangement());    //用车安排
				eaEntertainApply.setAccommodationArrangements(eaEntertainChange.getAccommodationArrangements());    //住宿安排
				eaEntertainApply.setPresentSouvenirs(eaEntertainChange.getPresentSouvenirs());    //赠送纪念品
				eaEntertainApply.setMealArrangementDate(eaEntertainChange.getMealArrangementDate());    //用餐安排_时间
				eaEntertainApply.setMealArrangementAddr(eaEntertainChange.getMealArrangementAddr());    //用餐安排_地点
				eaEntertainApply.setMealArrangementStandard(eaEntertainChange.getMealArrangementStandard());    //用餐安排_标准
				eaEntertainApply.setMealArrangementMealAttendants(eaEntertainChange.getMealArrangementMealAttendants());    //用餐安排_陪餐人员
				eaEntertainApply.setMealArrangementWorkingPersonnel(eaEntertainChange.getMealArrangementWorkingPersonnel());    //用餐安排_工作人员
				eaEntertainApply.setIsOfficialReceptionsOther(eaEntertainChange.getIsOfficialReceptionsOther());    //公务接待是否协助安排其他用餐
				eaEntertainApply.setIsBusinessReceptionsOther(eaEntertainChange.getIsBusinessReceptionsOther());    //商务接待除宴请外是否安排工作餐
				eaEntertainApply.setBudgetExpensesSupplementaryNotes(eaEntertainChange.getBudgetExpensesSupplementaryNotes());    //预算费用补充说明

				//获取招待费变更单维护的明细数据
				//删除相应招待费维护的明细
				Map<String,Object> params3 = new HashMap<String,Object>();
				params3.put("entertain_apply_id",eaEntertainApply.getId());
				eaHostObjectMapper.deleteByMap(params3);
				//删除相应活动明细
				Map<String,Object> activitiParams = new HashMap<String,Object>();
				activitiParams.put("pid",eaEntertainApply.getId());
				eaEntertainReceptionActivityMapper.deleteByMap(activitiParams);

				Map<String,Object> params2 = new HashMap<String,Object>();
				params2.put("entertain_apply_id",eaEntertainChange.getId());
				List<EaHostObject> eaHostObjectList = eaHostObjectMapper.selectByMap(params2);
				if(eaHostObjectList != null && eaHostObjectList.size() > 0){
					for (int i = 0; i < eaHostObjectList.size(); i++) {
						EaHostObject eaHostObject = eaHostObjectList.get(i);
						EaHostObject eaHostObject2 = new EaHostObject();
						BeanUtils.copyProperties(eaHostObject,eaHostObject2);

						//插入新的招待费明细
						eaHostObject2.setId(Uuid32Util.getUUid());
						eaHostObject2.setEntertainApplyId(eaEntertainApply.getId());
						eaHostObjectMapper.insert(eaHostObject2);
					}
				}

				Map<String,Object> params4 = new HashMap<String,Object>();
				params4.put("pid",eaEntertainChange.getId());
				List<EaEntertainReceptionActivity> eaEntertainReceptionActivities = eaEntertainReceptionActivityMapper.selectByMap(params4);
				if(eaEntertainReceptionActivities != null && eaEntertainReceptionActivities.size() > 0){
					for (int i = 0; i < eaEntertainReceptionActivities.size(); i++) {
						EaEntertainReceptionActivity eaEntertainReceptionActivity = eaEntertainReceptionActivities.get(i);
						EaEntertainReceptionActivity eaEntertainReceptionActivity2 = new EaEntertainReceptionActivity();
						BeanUtils.copyProperties(eaEntertainReceptionActivity,eaEntertainReceptionActivity2);

						//插入新的活动明细
						eaEntertainReceptionActivity2.setId(Uuid32Util.getUUid());
						eaEntertainReceptionActivity2.setPid(eaEntertainApply.getId());
						eaEntertainReceptionActivityMapper.insert(eaEntertainReceptionActivity2);
					}
				}
			}
			eaEntertainApplyMapper.updateById(eaEntertainApply);
		}

		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("form_id",eaEntertainChange.getId());
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap);
		if(eaTaskList != null && eaTaskList.size() > 0){
			EaTask eaTask = eaTaskList.get(0);
			//eaTask.setAuditManId(auditManId);
			//eaTask.setAuditMan(auditMan);
			eaTask.setAuditComment(auditComment);
			eaTask.setStatus(status);

			try {
				if(status.equals("3") || status.equals("2")){
					eaTask.setFinishTime(new Date());   //完成时间
				}
				eaTask.setAuditTime(new Date());    //审批时间
				eaTaskMapper.updateById(eaTask);

				returnMap.put("code","0");
				returnMap.put("message","审核成功!");
				return returnMap;
			}catch (Exception e){
				returnMap.put("code","-1");
				returnMap.put("message","系统异常,审核失败!");
				return returnMap;
			}
		}else{
			returnMap.put("code","-1");
			returnMap.put("message","待办不存在!");
			return returnMap;
		}
	}

}
